package naito_rescue.util.fb;

import rescuecore2.standard.components.*;
import rescuecore2.config.Config;
import rescuecore2.standard.entities.*;
import rescuecore2.standard.entities.StandardEntityConstants.*;
import rescuecore2.worldmodel.*;
import naito_rescue.task.job.*;
import naito_rescue.agent.*;
import java.util.*;
import java.io.*;

public class FireBrigadeUtils
{
	private static final double DEFAULT_WOODEN_IGNITION = 47.0f;
	private static final double DEFAULT_STEEL_IGNITION = 47.0f;
	private static final double DEFAULT_CONCRETE_IGNITION = 47.0f;
	private static String WOODEN_IGNITION_KEY = "resq-fire.wooden_ignition";
	private static String STEEL_IGNITION_KEY = "resq-fire.steel_ignition";
	private static String CONCRETE_IGNITION_KEY = "resq-fire.concrete_ignition";
	
	public static int updateBuildingPriority(NAITOAgent owner, Building target){
		StandardWorldModel model = owner.getWorldModel();
		int priority = Integer.MAX_VALUE / 2;
		int minus = 0;
		if(target.isGroundAreaDefined()){
			minus = target.getTotalArea()*9;
		}
		if(target.isFierynessDefined()){
			minus *= target.getFieryness();
		}
		if(owner.getLocation() instanceof Refuge){
			minus += model.getDistance(target.getID(), owner.getLocation().getID())/500;
		}else{
			minus += model.getDistance(target.getID(), owner.getID())/1000;
		}
		priority -= minus;

		int around = 0;
		int burning = 0;
		int test = target.isGroundAreaDefined()?(int)Math.sqrt(target.getGroundArea())*1000/2:20*1000;
		//System.out.println(test);
		Config config = owner.getConfig();
		for(Entity e : model.getObjectsInRange(target, test)){
			if(e instanceof Building){
				Building b = (Building)e;
				around++;
				if(b.isOnFire()){
					burning++;
				}else if(b.isFierynessDefined() && b.getFierynessEnum().equals(Fieryness.BURNT_OUT)){
					if(b.isTemperatureDefined() && b.getTemperature() > ignitionLevel(config, b)){
						burning++;
					}
				}
			}
		}
		//priority += distance/1000;
		priority = (int)(priority*(double)(1-(double)(burning/around)/2));
		return priority;
	}
	
	public static double ignitionLevel(Config config, Building b){
		double temp = 0;
		double woodenIgnition, steelIgnition, concreteIgnition;
		woodenIgnition = config.getFloatValue(WOODEN_IGNITION_KEY, DEFAULT_WOODEN_IGNITION);
		steelIgnition = config.getFloatValue(STEEL_IGNITION_KEY, DEFAULT_STEEL_IGNITION);
		concreteIgnition = config.getFloatValue(CONCRETE_IGNITION_KEY, DEFAULT_CONCRETE_IGNITION);
		
		if(b.isBuildingCodeDefined()){
			if(b.getBuildingCodeEnum().equals(BuildingCode.WOOD)){
				temp = woodenIgnition;
			}else if(b.getBuildingCodeEnum().equals(BuildingCode.STEEL)){
				temp = steelIgnition;
			}else if(b.getBuildingCodeEnum().equals(BuildingCode.CONCRETE)){
				temp = concreteIgnition;
			}
		}else{
			temp = 50;
		}
		return temp;
	}
	public static int calculateChargingNum(Building target) {
		int chargeNum;
		if(target.isGroundAreaDefined()){
			chargeNum = target.getGroundArea()/100 + 1;
		}else{
			chargeNum = 5; // マジックナンバー
		}
		if(target.isFierynessDefined()){
			chargeNum += target.getFieryness();
		}
		return chargeNum;
	}
}
